using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AirConsumeManager : Singleton<AirConsumeManager>
{
    private GameObject player => GameObject.FindWithTag("Player");

    private float heightDis;
    public float heightGap;

    public float gapConsume;
    public float airAmount;
    public bool stopConsume;

    private void OnEnable()
    {
        EventHandler.UpdateAir += OnUpdateAir;
        EventHandler.StartDive += OnStartDive;
        EventHandler.EndDive += OnEndDive;
    }

    private void OnDisable()
    {
        EventHandler.UpdateAir -= OnUpdateAir;
        EventHandler.StartDive -= OnStartDive;
        EventHandler.EndDive -= OnEndDive;
    }

    private void Start()
    {
        gapConsume = Settings.gapConsume;
        airAmount = Settings.airAmount;
    }

    private void Update()
    {
        UpdateGapConume();
    }

    private void UpdateGapConume()
    {
        heightDis = transform.position.y - player.transform.position.y;
        if (heightDis > heightGap)
        {
            gapConsume += 0.2f;
            heightGap += 5;
        }
        else if (heightDis < heightGap - 10)
        {
            gapConsume -= 0.2f;
            heightGap -= 5;
        }
    }
    
    private void OnStartDive()
    {
        airAmount = Settings.airAmount;
        stopConsume = false;
    }
    
    private void OnEndDive()
    {
        stopConsume = true;
    }
    
    private void OnUpdateAir()
    {
        if(airAmount > 0 && !stopConsume)
            airAmount -= gapConsume;
    }
}
